// pages/ctrl/ctrl.js
var value = 0
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		shose: [{
			device_name: "X5进化1"
		},
		{
			device_name: "X5进化2"
		},
		{
			device_name: "X5进化3"
		}
		],
		x: 0,
		y: 0,
		ox: 0,
		oy: 0,
		hd: 25,
		longTime: '',
		setTimeSpeed: 50,
		minus_url: "../../images/minus_.png",
		plus_url: "../../images/plus_.png",
		battery_url: "../../images/lb.png"
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this 
		// wx.setNavigationBarTitle({
		// 	title: that.data.shose[options.id].device_name,
		// })
		wx.createSelectorQuery().select("#progressView").boundingClientRect(function (rect) {
			that.data.ox = rect.width / 2;
			that.data.oy = rect.height / 2;
		}).exec();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		var that = this
		that.progressView = this.selectComponent("#progressView");
		that.progressView.drawProgressBar(that.data.hd, 100); //绘制环形进度
	},
	/* 加 */
	plus: function (e) {
		var value = this.data.hd;
		value += 1;
		if (value < 101) {
			this.data.hd = value;
			this.progressView.drawProgressBar(value, 100); //绘制环形进度
		}
	},
	/* 减 */
	minus: function (e) {
		var value = this.data.hd;
		value -= 1;
		if (value >= 0) {
			this.data.hd = value;
			this.progressView.drawProgressBar(value, 100); //绘制环形进度
		}
	},
	/* 减-长按-开始 */
	minusTouchStart: function (e) {
		var that = this;
		// that.setData({
		// 	minus_url: "../image/minus_.png"
		// });
	},
	/* 减-长按-结束 */
	minusTouchEnd: function (e) {
		var that = this;
		// that.setData({
		// 	minus_url: "../image/minus.png"
		// });
		clearInterval(that.data.longTime);
	},
	/* 减-长按-响应函数 */
	minusLongPress: function (e) {
		var that = this;
		that.data.longTime = setInterval(function () {
			var numVal = that.data.hd - 1;
			if (numVal >= 0) {
				that.progressView.drawProgressBar(numVal, 100); //绘制环形进度
				that.setData({
					hd: numVal
				});
			}
		}, that.data.setTimeSpeed);
	},
	/* 加-长按-开始 */
	plusTouchStart: function (e) {
		var that = this;
		// that.setData({
		// 	plus_url: "../image/plus_.png"
		// });
	},
	/* 加-长按-结束 */
	plusTouchEnd: function (e) {
		var that = this;
		// that.setData({
		// 	plus_url: "../image/plus.png"
		// });
		clearInterval(that.data.longTime);
	},
	/* 加-长按-响应函数 */
	plusLongPress: function (e) {
		var that = this;
		that.data.longTime = setInterval(function () {
			var numVal = that.data.hd + 1;
			if (numVal <= 100) {
				that.progressView.drawProgressBar(numVal, 100); //绘制环形进度
				that.setData({
					hd: numVal
				});
			}
		}, that.data.setTimeSpeed);
	},
	/* 进度条拖拉-开始 */
	touchStart: function (e) {

	},
	/* 进度条拖拉-响应函数 */
	touchMove: function (e) {
		var that = this;
		if (e.touches) {
			var et = e.touches[0];
		} else {
			var et = e;
		}
		wx.createSelectorQuery().select("#progressView").boundingClientRect(function (rect) {
			that.data.x = et.pageX - rect["left"];
			that.data.y = et.pageY - rect["top"];
		}).exec();
		if (that.data.x != 0 && that.data.y != 0) {
			var x = that.data.x - that.data.ox;
			var r = Math.atan2(that.data.x - that.data.ox, that.data.oy - that.data.y);
			if (r <= (Math.PI * 2 / 3) && r >= -(Math.PI * 2 / 3)) {
				var hd = 1.5 * (Math.PI * 2 / 3 + r) / (2 * Math.PI);
				value = 100 * hd;
				if (value >= 99.5) {
					value = 100;
				}
				if (value <= 0.5) {
					value = 0;
				}
				that.data.hd = value;
				that.progressView.drawProgressBar(value, 100);
			}
		}
	},
	/* 进度条拖拉-结束 */
	touchEnd: function (e) {

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})